<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box {
            width: 100px;
            height: 100px;
            background-color: aqua;
        }
    </style>
</head>

<body>
    <div id="box"></div>
</body>

<script>
    window.onload = function() {
        var box = document.getElementById('box');
        box.onmouseenter = function() {
            animation(box, 400);
        }
        box.onmouseleave = function() {
            animation(box, 100);
        }

        function animation(obj, target) {
            clearInterval(obj.time);
            obj.time = setInterval(() => {
                var speed = (target - obj.offsetWidth) / 8;
                speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
                if (obj.offsetWidth === target) {
                    clearInterval(obj.time);
                } else {
                    obj.style.width = obj.offsetWidth + speed + 'px';
                }
            }, 19);
        }
    }
</script>

</html>